// com.lesso.cyg.sunflower.infra.repository.impl.AttendanceRuleRepositoryImpl.java
package com.lesso.cyg.sunflower.infra.repository.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.cyg.sunflower.domain.repository.AttendanceRuleRepository;
import com.lesso.cyg.sunflower.infra.mapper.AttendanceRuleMapper;
import com.lesso.cyg.sunflower.model.AttendanceRule;
import com.lesso.cyg.sunflower.pojo.dto.AttendanceRuleQueryDTO;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * 考勤规则Repository实现类
 */
@Repository
public class AttendanceRuleRepositoryImpl implements AttendanceRuleRepository {

    @Resource
    private AttendanceRuleMapper attendanceRuleMapper;

    @Override
    public Integer save(AttendanceRule entity) {
        return attendanceRuleMapper.insert(entity);
    }

    @Override
    public void removeById(Long id) {
        attendanceRuleMapper.deleteById(id);
    }

    @Override
    public Integer update(AttendanceRule entity) {
        return attendanceRuleMapper.updateById(entity);
    }

    /**
     * 分页查询考勤规则
     */
    @Override
    public Page<AttendanceRule> selectPage(AttendanceRuleQueryDTO queryDTO) {
        // 计算分页起始位置（MyBatis-Plus页码从1开始）
        int pageNum = queryDTO.getPage().getCurrent();
        int pageSize = queryDTO.getPage().getSize();
        int offset = (pageNum - 1) * pageSize;

        // 查询当前页数据
        List<AttendanceRule> records = attendanceRuleMapper.selectByPage(
                offset,
                pageSize,
                queryDTO.getRuleCode(),
                queryDTO.getRuleName()
        );

        // 查询总条数
        Integer total = attendanceRuleMapper.selectCount(
                queryDTO.getRuleCode(),
                queryDTO.getRuleName()
        );

        // 封装分页结果
        Page<AttendanceRule> page = new Page<>(pageNum, pageSize);
        page.setRecords(records);
        page.setTotal(total);
        return page;
    }
}